package com.demo.youxuanmall.controller;

import com.demo.youxuanmall.common.Result;
import com.demo.youxuanmall.entity.Category;
import com.demo.youxuanmall.entity.User;
import com.demo.youxuanmall.service.CategoryService;
import com.demo.youxuanmall.service.SystemSettingService;
import com.demo.youxuanmall.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    private final UserService userService;
    private final CategoryService categoryService;

    public AdminController(SystemSettingService systemSettingService, UserService userService, CategoryService categoryService) {
        super(systemSettingService);
        this.userService = userService;
        this.categoryService = categoryService;
    }

    @GetMapping("")
    public String index() {
        return "admin/index";
    }

    // 统计页面
    @GetMapping("/statistics")
    public String statistics() {
        return "admin/statistics/dashboard";
    }

    @GetMapping("/profile")
    public String profile(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        return "admin/profile";
    }

    @PostMapping("/profile")
    @ResponseBody
    public Result<User> updateProfile(@RequestBody User user, HttpSession session) {
        return userService.updateProfile(user, session);
    }

    @PostMapping("/password")
    @ResponseBody
    public Result<?> updatePassword(@RequestBody User user, HttpSession session) {
        return userService.updatePassword(user, session);
    }

    @GetMapping("/categories")
    public String categoryList(HttpSession session, Model model) {
        try {
        // 检查是否是管理员
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return "redirect:/403";
        }
        
        // 获取所有分类
        List<Category> categories = categoryService.listAll();
        model.addAttribute("categories", categories);
        
        return "admin/category/list";
        } catch (Exception e) {
            log.error("加载分类管理页面出错", e);
            model.addAttribute("error", "加载分类管理页面失败：" + e.getMessage());
            return "error/500";
        }
    }

    @GetMapping("/products/edit")
    public String productEdit(HttpSession session, Model model) {
        try {
        // 检查是否是管理员
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return "redirect:/403";
        }
        
        // 获取所有分类
        List<Category> categories = categoryService.listAll();
        model.addAttribute("categories", categories);
        
        return "admin/product/edit";
        } catch (Exception e) {
            log.error("加载商品编辑页面出错", e);
            model.addAttribute("error", "加载商品编辑页面失败：" + e.getMessage());
            return "error/500";
        }
    }
} 